package com.example.mytestdemo.customviewbook

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * @author tgw
 * @date 2023/2/8
 * @describe
 */
class TestScaleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr) {

    var mPaint = Paint()
    var mPaintPoint = Paint()

    init {
        mPaint.style =  Paint.Style.STROKE
        mPaintPoint.style =  Paint.Style.FILL
        mPaintPoint.color = Color.BLUE
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //测试下，x，y的偏移
//        testScale(canvas)
        //错切的用法
//        testSkew(canvas)

        //其他功能函数之setSinCos
        testRotate(canvas)

    }

    private fun testScale(canvas: Canvas) {
        canvas.save()
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)
        var rectF = RectF(0f, 400f, 400f, 0f)
        var matrix = Matrix()
        //不知道为什么要使用postTranslate 否则不显示，使用这种类似方法使其偏移
        matrix.postTranslate((width / 2).toFloat(), (height / 2).toFloat())
        canvas.setMatrix(matrix)
        mPaint.setColor(Color.BLACK)
        canvas.drawRect(rectF, mPaint)
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)

        var matrix3 = Matrix(matrix)
        //见2.2.3.3　缩放中心点的作用，进行缩放后再次移动400，距离只想当于原先的200
        matrix.preScale(0.5f, 0.5f, 400f, 400f)
        canvas.setMatrix(matrix)
        mPaint.style = Paint.Style.FILL
        mPaint.setColor(Color.RED)
        canvas.drawRect(rectF, mPaint)
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)


        //负值 代表将其翻转，每次操作都是在上次的基础上,除非你保留原始的值
        matrix3.preScale(-0.5f, 1f)
        canvas.setMatrix(matrix3)
        mPaint.style = Paint.Style.STROKE
        mPaint.setColor(Color.RED)
        canvas.drawRect(rectF, mPaint)

        canvas.restore()
    }

    private fun testSkew(canvas: Canvas) {
        //●float kx：将原坐标点在X轴方向上移动一定的距离，即在Y轴方向上倾斜一定的角度，kx的值是倾斜角度的正切值。
        //
        //●float ky：同样地，将原坐标点在Y轴方向上移动一定的距离，即在X轴方向上倾斜一定的角度，ky的值是倾斜角度的正切值。
        canvas.save()
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)
        var rectF = RectF(0f, 100f, 100f, 0f)
        var matrix = Matrix()
        //不知道为什么要使用postTranslate 否则不显示，使用这种类似方法使其偏移
        matrix.postTranslate((width / 2).toFloat(), (height / 2).toFloat())
        canvas.setMatrix(matrix)
        mPaint.setColor(Color.BLACK)
        canvas.drawRect(rectF, mPaint)
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)

        var matrix3 = Matrix(matrix)
        //见2.2.4.2　
        matrix.preSkew(1f, 0f,0f,100f)
//        matrix.preSkew(0.5f, 0f)
        canvas.setMatrix(matrix)
        mPaint.style = Paint.Style.FILL
        mPaint.setColor(Color.RED)
        canvas.drawRect(rectF, mPaint)
        mPaintPoint.color = Color.YELLOW
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)



        canvas.restore()
    }

    /**
     *
     *2.2.5　其他功能函数之setSinCos
     */
    private fun testRotate(canvas: Canvas) {
        //●float sinValue：旋转角度的正弦值。
        //
        //●float cosValue：旋转角度的余弦值。
        //
        //●float px：旋转中心点的x坐标值。
        //
        //●float py：旋转中心点的y坐标值。
        canvas.save()
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)
        var rectF = RectF(0f, 100f, 300f, 0f)
        var matrix = Matrix()
        //不知道为什么要使用postTranslate 否则不显示，使用这种类似方法使其偏移
        matrix.postTranslate((width / 2).toFloat(), (height / 2).toFloat())
        canvas.setMatrix(matrix)
        mPaint.setColor(Color.BLACK)
        canvas.drawRect(rectF, mPaint)
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)

        //2.2.5.2　setSinCos函数的用法
        //组合Matrix
        var matrix2 = Matrix()
        matrix2.setSinCos(1f, 0f)
        matrix.preConcat(matrix2)

        //画矩形
        canvas.setMatrix(matrix)
        mPaint.style = Paint.Style.FILL
        mPaint.setColor(Color.RED)
        canvas.drawRect(rectF, mPaint)
        mPaintPoint.color = Color.YELLOW
        canvas.drawCircle(0f, 0f, 10f, mPaintPoint)



        canvas.restore()
    }
}